#!/bin/bash
# Copyright (C) 2016-2022 eGloo Incorporated
#
# This is free software, licensed under the GNU General Public License v3.

### BEGIN INIT INFO
# Provides: @PACKAGE_TARNAME@
# Required-Start: $local_fs $network $syslog
# Required-Stop: $local_fs $syslog
# Should-Start: $syslog
# Should-Stop: $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start up the @PACKAGE_NAME@
# Description: @PACKAGE_NAME@
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON="@sbindir@/@PACKAGE_TARNAME@"
NAME="@PACKAGE_TARNAME@"
PIDFILE="@volatilestatedir@/netifyd.pid"
DESC="@PACKAGE_NAME@"

unset TMPDIR

# Exit if the package is not installed
test -x $DAEMON || exit 0

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions

start() {
    # Load defaults.
    if [ -f @datadir@/functions.sh ]; then
        source @datadir@/functions.sh
        load_modules
    fi

    log_daemon_msg "Starting $DESC" "$NAME"

    NETIFYD_OPTS=$(auto_detect_options)

    start-stop-daemon --start --quiet --oknodo --pidfile "$PIDFILE" --exec $DAEMON -- $NETIFYD_OPTS
    status=$?
    log_end_msg $status
    return $status
}   

stop() {
    log_daemon_msg "Stopping $DESC" "$NAME"
    start-stop-daemon --stop --quiet --retry 5 --oknodo --pidfile "$PIDFILE" --name $NAME
    status=$?
    log_end_msg $status
    return $status
}

case "$1" in
    start)
        start
    ;;
    stop)
        stop
    ;;
    status)
        status_of_proc -p "$PIDFILE" "$DAEMON" "$NAME" && exit 0 || exit $?
    ;;
    restart)
        stop
        start
    ;;
    *)
    echo "Usage: $NAME {start|stop|status|restart}"
    exit 3
    ;;
esac
exit $RETVAL

# vi: expandtab shiftwidth=4 softtabstop=4 tabstop=4 syntax=sh
